<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

abstract class Bean {

  public function __call($name, $arguments) {
    if (strlen($name)>3) {
      $property = strtolower(substr($name, 3, 1)) . substr($name, 4);
      $action = substr($name, 0, 3);
      if (in_array($action, array("get", "set"))) {
        if (property_exists($this, $property)) {
          if ($action==="get") {
              return $this->$property;
          } else {
              return $this->$property = $arguments[0];
          }
        } else {
          throw new Exception("Property $property does not exist");
        }
      } else {
        throw new Exception("Method $name does not exist");
      }
    } else {
      throw new Exception("Method $name does not exist");
    }
  }
/*
  public function __get($name) {
    $getter = 'get' . ucfirst($name);
    if (method_exists($this, $getter)) {
      return call_user_func(array($this, $getter));
    }
    return $this->$name;
  }

  public function __set($name, $value) {
    $setter = 'set' . ucfirst($name);
    if (method_exists($this, $setter)) {
      call_user_func(array($this, $setter), $value);
    } else {
      $this->$name = $value;
    }
  }
  */

}

class UserBean extends Bean {

  protected $username, $email, $location;

}

$user = new UserBean();
$user->username = 'Jim';
echo $user->getUsername1();
?>
